//
//  EMini.swift
//  RN
//
//  Created by HFY on 2021/1/30.
//

import UIKit

@objc(EMiniEvent)
class EMiniEvent: NSObject {
    
    @objc(callNative:args:callBack:)
    func callNative(funcName: String, args: [String:Any],callBack:RCTResponseSenderBlock) -> Void {
        debugPrint("----->:",self)

        debugPrint("----->:",funcName,args)
        DispatchQueue.main.async {
            if funcName == "push" {
                self.push(args)
            }else{
                let currentRN = SReactViewManager.shared.getRN(rnID: (args["app"]! as! [String:Any])["appID"] as! String)
                let rn = SReactViewController.init(rnName: "App", props: ["scores":
                                                                                    [
                                                                                        ["name":"Alex", "value":"42"],
                                                                                        ["name":"Joel", "value":"10"]
                                                                                    ]
                                                                                ])
                rn.debuger.isDebug = true
                currentRN?.navigationController?.pushViewController(rn, animated: true)
            }
        }
    }
    
    func push(_ args:[String:Any]) {
        
        do{
            let path = Bundle.main.url(forResource: "buz.ios", withExtension: "bundle")!
            let data = try Data.init(contentsOf: path)
            LoadBundle.shared.bridge?.batched.executeSourceCode(data, sync: true)
        }catch {
            
        }
        let currentRN = SReactViewManager.shared.getRN(rnID: (args["app"]! as! [String:Any])["appID"] as! String)
        let rn = SReactViewController.init(rnName: "App", props: ["scores":
                                                                            [
                                                                                ["name":"Alex", "value":"42"],
                                                                                ["name":"Joel", "value":"10"]
                                                                            ]
                                                                        ])
        rn.debuger.isDebug = true
        currentRN?.navigationController?.pushViewController(rn, animated: true)
    }

}
